package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WfUser;
import com.ruoyi.system.mapper.WfUserMapper;
import com.ruoyi.system.service.IWfUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * wifi微信用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class WfUserServiceImpl extends ServiceImpl<WfUserMapper, WfUser> implements IWfUserService {
    @Autowired
    private WfUserMapper wfUserMapper;

    /**
     * 查询wifi微信用户信息
     *
     * @param userId wifi微信用户信息主键
     * @return wifi微信用户信息
     */
    @Override
    public WfUser selectWfUserByUserId(Long userId) {
        return wfUserMapper.selectWfUserByUserId(userId);
    }

    @Override
    public WfUser selectWfUserByOpenId(String openId) {
        Assert.notEmpty(openId, "openId 不能为空");
        return wfUserMapper.selectOne(new LambdaQueryWrapper<WfUser>().eq(WfUser::getOpenId, openId));
    }

    /**
     * 查询wifi微信用户信息列表
     *
     * @param wfUser wifi微信用户信息
     * @return wifi微信用户信息
     */
    @Override
    public List<WfUser> selectWfUserList(WfUser wfUser) {
        return wfUserMapper.selectWfUserList(wfUser);
    }

    /**
     * 新增wifi微信用户信息
     *
     * @param wfUser wifi微信用户信息
     * @return 结果
     */
    @Override
    public int insertWfUser(WfUser wfUser) {
        wfUser.setCreateTime(DateUtils.getNowDate());
        return wfUserMapper.insertWfUser(wfUser);
    }

    /**
     * 修改wifi微信用户信息
     *
     * @param wfUser wifi微信用户信息
     * @return 结果
     */
    @Override
    public int updateWfUser(WfUser wfUser) {
        wfUser.setUpdateTime(DateUtils.getNowDate());
        return wfUserMapper.updateWfUser(wfUser);
    }

    /**
     * 批量删除wifi微信用户信息
     *
     * @param userIds 需要删除的wifi微信用户信息主键
     * @return 结果
     */
    @Override
    public int deleteWfUserByUserIds(Long[] userIds) {
        return wfUserMapper.deleteWfUserByUserIds(userIds);
    }

    /**
     * 删除wifi微信用户信息信息
     *
     * @param userId wifi微信用户信息主键
     * @return 结果
     */
    @Override
    public int deleteWfUserByUserId(Long userId) {
        return wfUserMapper.deleteWfUserByUserId(userId);
    }

    @Override
    public void updateWfUserPhone(String phoneNumber, String openId) {
        WfUser wfUser = wfUserMapper.selectOne(new LambdaQueryWrapper<WfUser>().eq(WfUser::getOpenId, openId));
        if (wfUser == null) {
            throw new ServiceException("微信用户不存在");
        }
        wfUser.setPhoneNumber(phoneNumber);
        wfUserMapper.updateWfUser(wfUser);
    }

    @Override
    public boolean save(WfUser entity) {
        return super.save(entity);
    }
}
